package com.lt.flashcard.view;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zhtml.Messagebox;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.ActionResult;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.Partner;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.ImportDataService;
import com.lt.flashcard.model.service.Repository;

public class AdminCodeImportVm {
	
	private List<String[]> filelist = new ArrayList<String[]>();
	private List<Object[]> fcslist = new ArrayList<Object[]>();
	private List<Partner> partnerlist = new ArrayList<Partner>();
	private final String IMPORT_PATH = Utility.getWebAppPath() + Utility.getConfiguration("com.lt.flashcard.flashcardFilesToImportFolder","files_to_import");
	
	private Set<Object[]> slFcs;
	private int slFl = -1;
	private int slPartner = -1;
	private Object slFcs1;
	
	private int coin = 0;
	
	@Init
	public void init() {
		AAService srv  = Repository.getInstance().getAAService();
		if(!srv.isLogin() || !srv.hasRole("admin")) {
			Utility.adminLoginOrAccessDenied(false);
		}
		
		buildFileList();
		//load flashcardset list
		Session ses = Repository.getInstance().getCurrentSession();
		Query query = ses.createQuery("select id, name from FlashcardSet where admin = :admin order by name");
		query.setParameter("admin", FlashcardSet.ADMIN_YES);
		setFcslist(query.list());
		//load partner list
		partnerlist = Repository.getInstance().getEcommerceService().getPartners();
	}
	
	@Command
	public void save() {
		if(slFcs == null && coin <= 0) {
			Messagebox.show("Choose product or coin or both");
			return;
		}
		Map<Long, String> fcsid = new HashMap<Long, String>();
		if(slFcs != null)
			for(Object[] fcs : slFcs) {
				fcsid.put((Long) fcs[0], (String) fcs[1]);
			}
		
		Partner partner = null;
		try { partner = partnerlist.get(slPartner); }
		catch (Exception e) { }
		
		String filename = String.valueOf(filelist.get(slFl)[0]).trim();
		ImportDataService srv = Repository.getInstance().getImportDataService();
		ActionResult rs = srv.importProductCodeFromExcel(filename, fcsid, partner, coin);
		if(rs.isError()) {
			Messagebox.show(rs.getMessage().length() > 200 ? rs.getMessage().substring(0, 200) : rs.getMessage());
			return;
		}
		else {
			Utility.redirect("admin_code.zul", true);
		}
	}
	
	public void buildFileList() {
		filelist.clear();
		File[] files = new File(IMPORT_PATH).listFiles();
		Arrays.sort(files);
		for(File f : files) {
			if(f.getName().toLowerCase().endsWith(".xls")) {
				filelist.add(new String[] { f.getName(), f.getAbsolutePath() });
			}
		}
	}
	
	public List<String[]> getFilelist() {
		return filelist;
	}

	public void setFilelist(List<String[]> filelist) {
		this.filelist = filelist;
	}

	/**
	 * @return the fcslist
	 */
	public List<Object[]> getFcslist() {
		return fcslist;
	}

	/**
	 * @param fcslist the fcslist to set
	 */
	public void setFcslist(List<Object[]> fcslist) {
		this.fcslist = fcslist;
	}

	/**
	 * @return the slFcs
	 */
	public Set<Object[]> getSlFcs() {
		return slFcs;
	}

	/**
	 * @param slFcs the slFcs to set
	 */
	public void setSlFcs(Set<Object[]> slFcs) {
		this.slFcs = slFcs;
	}

	/**
	 * @return the slFl
	 */
	public int getSlFl() {
		return slFl;
	}

	/**
	 * @param slFl the slFl to set
	 */
	public void setSlFl(int slFl) {
		this.slFl = slFl;
	}

	/**
	 * @return the slFcs1
	 */
	public Object getSlFcs1() {
		return slFcs1;
	}

	/**
	 * @param slFcs1 the slFcs1 to set
	 */
	public void setSlFcs1(Object slFcs1) {
		this.slFcs1 = slFcs1;
	}

	/**
	 * @return the partnerlist
	 */
	public List<Partner> getPartnerlist() {
		return partnerlist;
	}

	/**
	 * @param partnerlist the partnerlist to set
	 */
	public void setPartnerlist(List<Partner> partnerlist) {
		this.partnerlist = partnerlist;
	}

	/**
	 * @return the slPartner
	 */
	public int getSlPartner() {
		return slPartner;
	}

	/**
	 * @param slPartner the slPartner to set
	 */
	public void setSlPartner(int slPartner) {
		this.slPartner = slPartner;
	}

	/**
	 * @return the coin
	 */
	public int getCoin() {
		return coin;
	}

	/**
	 * @param coin the coin to set
	 */
	public void setCoin(int coin) {
		this.coin = coin;
	}
}
